//
//  XWJChangeSoureCell.swift
//  DZMeBookRead
//
//  Created by fox on 2017/8/30.
//  Copyright © 2017年 DZM. All rights reserved.
//

import UIKit
class PastTimeFormatter: Formatter {

    static let calendar = Calendar(identifier: Calendar.Identifier.iso8601)

    static let shared = PastTimeFormatter()

    override func string(for obj: Any?) -> String? {
        guard let date = obj as? Date else {
            return nil
        }
        let dc = PastTimeFormatter.calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date, to: Date())
        if let y = dc.year, y >= 1 {
            return "\(y)年前"
        } else if let m = dc.month, m >= 1 {
            return "\(m)月前"
        } else if let d = dc.day, d >= 1 {
            return "\(d)天前"
        } else if let h = dc.hour, h >= 1 {
            return "\(h)小时前"
        } else if let m = dc.minute, m >= 10 {
            return "\(m)分钟前"
        } else {
            return "刚刚"
        }
    }
}
class XWJChangeSoureCell: UITableViewCell {
   
    var model:ChangeSoureModel? {
        didSet{
            if let model = model {
                id = model.id
                lastChapterLab.text = model.lastChapter
                updatedLab.text = model.updated
                let timeStr = model.updated
                let time = (timeStr as NSString).substring(to: 19)
                let dateString  = self.sinaTimeStrToDate(sinaTimeStr: time)
                let str  = PastTimeFormatter.shared.string(for: dateString)
                updatedLab.text = str
            }
        }
    }
    
    var currenSele  = UILabel()
    var lastChapterLab = UILabel()
    var updatedLab = UILabel()
    var id:String = ""
    
  
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    func sinaTimeStrToDate (sinaTimeStr: String) -> NSDate {
        let formatStr = "yyyy-MM-dd'T'HH:mm:ss"
        hmdateFormatter.dateFormat = formatStr
        hmdateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 60*60*8) as TimeZone!
        //下面的这个属性必须传,不然在真机上时间可能不准确
        hmdateFormatter.locale = NSLocale(localeIdentifier: "en") as Locale!
        return hmdateFormatter.date(from: sinaTimeStr)! as NSDate
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        updatedLab.frame = CGRect.init(x: 12, y: 8, width: ScreenWidth, height: 24)
        lastChapterLab.frame = CGRect.init(x: 12, y: updatedLab.frame.maxY + 8, width: ScreenWidth - 84, height: 24)
        currenSele.frame = CGRect.init(x: ScreenWidth - 100, y: 15, width: 60, height: 20)
        currenSele.centerY = contentView.centerY
    
        
        
    }
    
    func setupUI() {
        contentView.addSubview(lastChapterLab)
        contentView.addSubview(updatedLab)
        contentView.addSubview(currenSele)
        lastChapterLab.text = "第一百八十章 满纸荒唐言，一把辛酸泪!"
        updatedLab.text = "21小时前"
        currenSele.text = "当前选中"
        currenSele.font = UIFont.systemFont(ofSize: 14)
        lastChapterLab.font = UIFont.systemFont(ofSize: 14)
        currenSele.isHidden = true
        currenSele.textColor = UIColor.red
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
